package com.xxhui.scrolldemo.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by hui on 2016/7/23.
 */
public class DrawViewByLayoutParams extends View {

    private static final String TAG = "DrawViewByLayoutMethod2" ;
    private int lastX;
    private int lastY;
    private int viewWidth;
    private int viewHeight;
    private Paint paint;

    public DrawViewByLayoutParams(Context context) {
        super(context);
        initRes();
    }

    public DrawViewByLayoutParams(Context context, AttributeSet attrs) {
        super(context, attrs);
        initRes();
    }

    public void initRes(){
        // 给View设置背景颜色，便于观察
        setBackgroundColor(Color.YELLOW);
        paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.BLACK);
        paint.setTextSize(20);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();//获取以当前View为参考系的x坐标
        int y = (int) event.getY();//获取以当前View为参考系的y坐标
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                // 记录触摸点坐标
                lastX = x;
                lastY = y;

                break;
            case MotionEvent.ACTION_MOVE:
                int offsetX = x - lastX;
                int offsetY = y - lastY;
                //使用布局的layoutParams需要与布局相对应，使用viewGroup的MarginLayoutParams就不用管布局如何
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
                //LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
                layoutParams.leftMargin +=offsetX;
                layoutParams.topMargin += offsetY;
                setLayoutParams(layoutParams);//记得设置，不然不会动
                break;
            case MotionEvent.ACTION_UP:
                offsetX = x - lastX;
                offsetY = y - lastY;
                //layout(0,getTop(),getRight()-getLeft(),getBottom());
                layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
                //LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
                layoutParams.leftMargin =0;
                //layoutParams.topMargin += offsetY;
                setLayoutParams(layoutParams);//记得设置，不然不会动
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.translate(viewWidth,viewHeight);
        canvas.drawText("LayoutParams()",0,viewHeight/2,paint);
    }
}
